package com.jprocms.module.cms.content;

import cn.hutool.core.collection.CollUtil;
import com.jpro.framework.security.core.util.SecurityFrameworkUtils;
import com.jprocms.module.cms.dal.dataobject.content.ContentDO;
import com.jprocms.module.cms.dal.dataobject.site.SiteDO;
import com.jprocms.module.cms.dal.mysql.content.ContentMapper;
import com.jprocms.module.cms.es.dao.EsContentRepo;
import com.jprocms.module.cms.framework.enums.EnumContentEvent;
import com.jprocms.module.cms.service.staticpage.StaticHtmlProducer;
import com.jprocms.module.cms.util.RequestUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static com.jprocms.module.cms.dal.dataobject.content.ContentDO.REQUEST_ATTR_CONTENT_ORIGIN_CHANNEL_ID;
import static com.jprocms.module.cms.dal.dataobject.content.ContentDO.REQUEST_ATTR_CONTENT_ORIGIN_CHANNEL_ID_MAP;
import static com.jprocms.module.cms.dal.dataobject.task.TaskDO.TASK_NAME_CONTENT_AUTO;

/**
 * {@link ContentAppEvent} 的监听器
 * 实现内容同步生成静态页功能
 *
 * @author jprocms
 */
@Slf4j
@Component
public class ContentStaticListener implements ApplicationListener<ContentAppEvent> {

    @Resource
    private ContentMapper contentMapper;
    @Resource
    private EsContentRepo esContentRepo;
    @Resource
    private StaticHtmlProducer staticHtmlProducer;

    @Override
    public void onApplicationEvent(ContentAppEvent event) {
        if (event.getId() != null) {
            //单条操作
            ContentDO contentDO = contentMapper.selectByIdIgnoreDel(event.getId());
            if (contentDO == null) {
                return;
            }
            //新建内容不为空则根据栏目配置的采集规则自动推送
            contentStaticAsync(event);
        } else {
            //批量删除或者批量发布
            contentStaticAsync(event);
        }
    }

    private void contentStaticAsync(ContentAppEvent event) {
        final Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        if (EnumContentEvent.delete.equals(event.getEvent())) {
            List<Long> ids = event.getIds();
            final List<ContentDO> contentDOS = contentMapper.selectByContentIdsIgnoreDel(ids);
            if (CollectionUtils.isNotEmpty(contentDOS)) {
                final SiteDO site = contentDOS.get(0).getSite();
                if (site.getEnabledAndAuto()) {
                    staticHtmlProducer.updateContentRelatedHtml(site.getId(), loginUserId, TASK_NAME_CONTENT_AUTO, contentDOS, null);
                }
            }
        } else if (EnumContentEvent.create.equals(event.getEvent())) {
            final ContentDO contentDO = contentMapper.selectByIdIgnoreDel(event.getId());
            final SiteDO site = contentDO.getSite();
            if (site.getEnabledAndAuto()) {
                staticHtmlProducer.updateContentRelatedHtml(site.getId(), loginUserId, TASK_NAME_CONTENT_AUTO, Arrays.asList(contentDO), null);
            }
        } else if (EnumContentEvent.update.equals(event.getEvent())) {
            if (CollUtil.isNotEmpty(event.getIds())) {
                final List<ContentDO> contentDOS = contentMapper.selectByContentIdsIgnoreDel(event.getIds());
                if (CollectionUtils.isNotEmpty(contentDOS)) {
                    final SiteDO site = contentDOS.get(0).getSite();
                    final Map<Long, Long> contentOriginChannelMap = (Map<Long, Long>) RequestUtils.getHttpServletRequest().getAttribute(REQUEST_ATTR_CONTENT_ORIGIN_CHANNEL_ID_MAP);
                    if (site.getEnabledAndAuto()) {
                        for (ContentDO contentDO : contentDOS) {
                            Long originContentId = contentOriginChannelMap != null ? contentOriginChannelMap.get(contentDO.getId()) : null;
                            staticHtmlProducer.updateContentRelatedHtml(site.getId(), loginUserId, TASK_NAME_CONTENT_AUTO, Arrays.asList(contentDO), originContentId);
                        }
                    }
                }
            } else {
                final ContentDO contentDO = contentMapper.selectByIdIgnoreDel(event.getId());
                final SiteDO site = contentDO.getSite();
                if (site.getEnabledAndAuto()) {
                    Long originContentId = (Long) RequestUtils.getHttpServletRequest().getAttribute(REQUEST_ATTR_CONTENT_ORIGIN_CHANNEL_ID);
                    staticHtmlProducer.updateContentRelatedHtml(site.getId(), loginUserId, TASK_NAME_CONTENT_AUTO, Arrays.asList(contentDO), originContentId);
                }
            }
        }
    }


}
